window.onload = function () {
    InitTimeDisplay();
}

function ShowFileDialogOverlay() {
    let mask = document.getElementById("file-dialog");
    mask.style.display = "block";
    document.documentElement.style.overflow = "hidden";
}

function CloseFileDialog() {
    let mask = document.getElementById("file-dialog");
    mask.style.display = "none";
    document.documentElement.style.overflow = "auto";
}

function ShowFileDeleteDialogOverlay() {
    let mask = document.getElementById("file-delete-dialog");
    mask.style.display = "block";
    document.documentElement.style.overflow = "hidden";
}

function CloseFileDeleteDialog() {
    let mask = document.getElementById("file-delete-dialog");
    mask.style.display = "none";
    document.documentElement.style.overflow = "auto";
}

function GetFile() {
    let urlTextBox = document.getElementsByClassName("content-box")[0].getElementsByClassName("input")[0];
    if (urlTextBox.value === "") {
        ShowAlertOverlay("文件Url不能为空.");
        return;
    }
    if (!urlTextBox.value.startsWith("/"))
        urlTextBox.value = "/" + urlTextBox.value
    let path = btoa(urlTextBox.value);
    let url = baseUrl + "/manage/file/" + path;
    let http = new XMLHttpRequest();
    http.open("GET", url);
    http.send();
    http.onreadystatechange = function () {
        if (this.readyState === http.DONE) {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status) {
                case 0:
                    ShowAlertOverlay("无法连接到远端服务器!");
                    CloseLoadingOverlay();
                    break;
                case 200:
                    let descBox = document.getElementsByClassName("content-box")[0].getElementsByClassName("desc")[0];
                    let downloadButton = document.getElementById("download-button");

                    let cells = resJson["Result"]["cells"];
                    for (let i = 0; i < cells.length; i++) {
                        let cell = cells[i];
                        if (cell.column === "desc") {
                            descBox.value = cell.value;
                            console.log(cell.value);
                            descBox.disabled = true;
                            downloadButton.onclick = function () {
                                window.open(baseUrl + "/file/" + path);
                            }
                        }
                    }

                    if (resJson.Warning.length !== 0) {
                        for (let i = 0; i < resJson.Warning.length; i++) {
                            displayText += resJson.Warning[i];
                            displayText += "<br/>";
                        }
                        ShowAlertOverlay(displayText);
                    }
                    CloseLoadingOverlay();
                    break;
                default:
                    displayText += "Code: ";
                    displayText += this.status;
                    displayText += " ";
                    displayText += this.statusText;
                    displayText += "<br/>";
                    for (let i = 0; i < resJson.Warning.length; i++) {
                        displayText += resJson.Warning[i];
                        displayText += "<br/>";
                    }
                    displayText += resJson.Error;
                    displayText += "<br/>";
                    ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
            }
        }
    };
    ShowLoadingOverlay();
}

function DelFile() {
    let urlTextBox = document.getElementsByClassName("file-delete-content")[0].getElementsByClassName("input")[0];
    if (urlTextBox.value === "") {
        ShowAlertOverlay("文件Url不能为空.");
        return;
    }
    if (!urlTextBox.value.startsWith("/"))
        urlTextBox.value = "/" + urlTextBox.value
    let path = btoa(urlTextBox.value);
    let url = baseUrl + "/manage/file";
    let http = new XMLHttpRequest();
    http.onreadystatechange = function () {
        if (this.readyState === http.DONE) {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status) {
                case 0:
                    ShowAlertOverlay("无法连接到远端服务器!");
                    CloseLoadingOverlay();
                    break;
                case 200:
                    if (resJson.Warning.length !== 0) {
                        for (let i = 0; i < resJson.Warning.length; i++) {
                            displayText += resJson.Warning[i];
                            displayText += "<br/>";
                        }
                        ShowAlertOverlay(displayText);
                    } else {
                        ShowOkOverlay();
                    }
                    CloseLoadingOverlay();
                    break;
                default:
                    displayText += "Code: ";
                    displayText += this.status;
                    displayText += " ";
                    displayText += this.statusText;
                    displayText += "<br/>";
                    for (let i = 0; i < resJson.Warning.length; i++) {
                        displayText += resJson.Warning[i];
                        displayText += "<br/>";
                    }
                    displayText += resJson.Error;
                    displayText += "<br/>";
                    ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
            }
        }
    };

    let delFile = new XMLHttpRequest();
    delFile.open("DELETE", baseUrl + "/file/" + path);
    delFile.send();
    delFile.onreadystatechange = function () {
        if (this.readyState === http.DONE) {
            if (this.status === 200) {
                http.open("DELETE", url);
                http.send(`{"tableName":"file","rows":[{"rowKey":"` + path + `","column":"*"}]}`);
            } else {
                ShowAlertOverlay("文件删除失败.");
                CloseLoadingOverlay();
            }
        }
    }
    ShowLoadingOverlay();
}

function PushFile() {
    let urlTextBox = document.getElementsByClassName("file-dialog-window-content")[0].getElementsByClassName("input")[0];
    let descTextBox = document.getElementsByClassName("file-dialog-window-content")[0].getElementsByClassName("desc")[0];
    let fileInput = document.getElementsByClassName("file-dialog-window-content")[0].getElementsByClassName("file")[0];
    if (urlTextBox.value === "") {
        ShowAlertOverlay("文件Url不能为空.");
        return;
    }
    if (!urlTextBox.value.startsWith("/"))
        urlTextBox.value = "/" + urlTextBox.value
    let path = btoa(urlTextBox.value);
    if (descTextBox.value === "") {
        ShowAlertOverlay("文件描述不能为空.");
        return;
    }
    if (fileInput.value === "") {
        ShowAlertOverlay("请选择文件.");
        return;
    }

    let fileUpload = new XMLHttpRequest();
    fileUpload.open("PUT", baseUrl + "/file/" + path);
    fileUpload.onreadystatechange = function () {
        if (this.readyState === fileUpload.DONE) {
            if (this.status === 200) {
                ShowOkOverlay();
                CloseLoadingOverlay();
            } else {
                ShowAlertOverlay("上传文件失败.");
                CloseLoadingOverlay();
            }
        }
    }
    let data =
        {
            tableName: "file",
            rows: []
        };

    let row =
        {
            rowKey: path,
            columns: []
        };

    let column =
        {
            column: "desc",
            value: descTextBox.value,
            canSearch: true,
            canCut: true
        };
    let column2 =
        {
            column: "url",
            value: urlTextBox.value,
            canSearch: false,
            canCut: false
        };
    row.columns.push(column);
    row.columns.push(column2);
    data.rows.push(row);
    let url = baseUrl + "/manage/" + data.tableName;
    let dataBody = JSON.stringify(data);
    let http = new XMLHttpRequest();
    http.open("PUT", url);
    http.onreadystatechange = function () {
        if (this.readyState === http.DONE) {
            let resJson = JSON.parse(this.responseText);
            let displayText = "";
            switch (this.status) {
                case 0:
                    ShowAlertOverlay("无法连接到远端服务器!");
                    CloseLoadingOverlay();
                    break;
                case 200:
                    if (resJson.Warning.length !== 0) {
                        for (let i = 0; i < resJson.Warning.length; i++) {
                            displayText += resJson.Warning[i];
                            displayText += "<br/>";
                        }
                        ShowAlertOverlay(displayText);
                    }
                    else {
                    }
                    fileUpload.overrideMimeType("application/octet-stream");
                    fileUpload.send(reader.result);
                    break;
                default:
                    displayText += "Code: ";
                    displayText += this.status;
                    displayText += " ";
                    displayText += this.statusText;
                    displayText += "<br/>";
                    for (let i = 0; i < resJson.Warning.length; i++) {
                        displayText += resJson.Warning[i];
                        displayText += "<br/>";
                    }
                    displayText += resJson.Error;
                    displayText += "<br/>";
                    ShowAlertOverlay(displayText);
                    CloseLoadingOverlay();
                    break;
            }
        }
    };
    let reader = new FileReader();
    reader.readAsArrayBuffer(fileInput.files[0])
    reader.onload = function(){
        http.send(dataBody);
    }
    ShowLoadingOverlay();
}